package com.lnw.travels.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lnw.travels.entity.Result;
import com.lnw.travels.entity.TProvince;
import com.lnw.travels.service.ProvinceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("province")
@CrossOrigin//允许跨域
@Slf4j
public class ProvinceController {

    @Autowired
    private ProvinceService provinceService;

    /**
     * 修改省份
     * @param province
     * @return
     */
    @PostMapping("update")
    public Result updateProvince(@RequestBody TProvince province){
        Result result = new Result();
        try{
            //判断所修改的省份是否存在
            QueryWrapper<TProvince> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name",province.getName());
            TProvince tProvince = provinceService.getOne(queryWrapper);
            if (tProvince==null||tProvince.getId() == province.getId()){//该省份不存在
                //可以进行修改
                if (provinceService.saveOrUpdate(province)){
                    result.setStatus(true).setMsg("修改成功");
                }else{
                    result.setStatus(false).setMsg("信息修改失败");
                }
            }else{
                //该省份存在
                result.setStatus(false).setMsg("该省份已存在");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setStatus(false).setMsg(e.getMessage());
        }

        return result;

    }

    /**
     * 根据省份id查询省份
     * @param id
     * @return
     */
    @GetMapping("findOneProvince")
    public TProvince findProvinceById(String id){
        QueryWrapper<TProvince> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return provinceService.getOne(queryWrapper);
    }

    /**
     * 保存省份信息,首先判断该省份是否存在
     * @param province
     * @return
     */
    @PostMapping("save")
    public Result saveProvince(@RequestBody TProvince province){
        Result result = new Result();
        try{
            //判断所添加的省份是否存在
            QueryWrapper<TProvince> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name",province.getName());
            TProvince tProvince = provinceService.getOne(queryWrapper);
            if (tProvince==null){//该省份不存在
                province.setPlacecounts(0);
                boolean save = provinceService.save(province);
                if (save){
                    result.setMsg("保存成功");
                }else{
                    result.setStatus(false).setMsg("保存失败");
                }
            }else{
                //该省份存在
                result.setStatus(false).setMsg("该省份已存在");
            }

        }catch (Exception e){
            e.printStackTrace();
            result.setStatus(false).setMsg(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据id删除省份
     * @param id
     * @return
     */
    @GetMapping("delete")
    public Result deleteProvince(String id){
        Result result = new Result();
        System.out.println(id);
        try {
            boolean isDelete = provinceService.removeById(id);
            if (isDelete){
                result.setMsg("删除成功");
            }else{
                result.setStatus(false).setMsg("删除失败");
            }

        }catch (Exception e){
            e.printStackTrace();
            result.setStatus(false).setMsg(e.getMessage());
        }
        return result;
    }

    /**
     * 查询所有省份信息
     * @return
     */
    @GetMapping("allProvince")
    public Map<String,Object> getAllProvince(Integer page, Integer size){

        HashMap<String, Object> map = new HashMap<>();
        IPage<TProvince> provinceIPage = new Page<>();
        //每页条数
        provinceIPage = provinceService.selectPage(page,size);
        long totals = provinceIPage.getTotal();
        long totalPage = totals%size==0?totals/size:totals/size+1;
        map.put("province",provinceIPage.getRecords());
        map.put("totals",totals);
        map.put("totalPage",totalPage);
        map.put("page",provinceIPage.getPages());
        return map;

    }
}
